# @configure_input@
# --------------------------------------------------------------------------
# Makefile for Hugs interpreter
# (this should be a POSIX 1003.2-1992 Makefile)
#
# The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
# Yale Haskell Group, and the OGI School of Science & Engineering at OHSU,
# 1994-2004, All rights reserved.  It is distributed as
# free software under the license in the file "License", which is
# included in the distribution.
# --------------------------------------------------------------------------

# Targets:
#
# <default>:   	  make hugs@EXEEXT@ and runhugs@EXEEXT@
# install:	  make and install programs
# hugs@EXEEXT@:	  make minimal working system
# runhugs@EXEEXT@: make batch-mode version of Hugs
# corehugs@EXEEXT@: make Hugs core generator from loaded files
# clean:       	  delete files not required in running system
# distclean:      delete files that can be regenerated using C compiler
# veryclean:      delete all machine generated files 
#                   (you need perl, bison/yacc, etc to rebuild these files)
# TAGS:           build emacs TAGS table

CC		= @CC@
CFLAGS		= @CFLAGS@ @DEBUGFLAGS@
OPTFLAGS	= @OPTFLAGS@

LD		= @LD@
LDFLAGS    	= @LDFLAGS@  @LDDEBUGFLAGS@
LIBS		= @LIBS@

YACC		= @YACC@
RM		= @RM@
CP		= @CP@
EXEEXT		= @EXEEXT@
OBJEXT		= @OBJEXT@
STRIP		= @STRIP@

# These variables determine where various parts of the Hugs system are
# installed.  (They are ignored in Windows or DOS.)
# Binaries are installed in $(bindir); libraries go in $(hugsdir)/libraries
prefix		= @prefix@
exec_prefix	= @exec_prefix@
bindir		= @bindir@
libdir		= @libdir@
datarootdir	= @datarootdir@
datadir		= @datadir@
mandir          = @mandir@
hugsdir		= @hugsdir@

.SUFFIXES	:
.SUFFIXES	: .c .h .$(OBJEXT)

HFILES		= HsFFI.h builtin.h char.h command.h config.h connect.h \
		  errors.h evaluator.h goal.h machdep.h module.h observe.h \
		  options.h opts.h output.h prelude.h script.h server.h \
		  storage.h strutil.h subst.h
CFILES		= hugs.c runhugs.c corehugs.c server.c edit.c observe.c \
		  builtin.c char.c compiler.c errors.c evaluator.c ffi.c \
		  goal.c input.c machdep.c machine.c module.c opts.c \
		  output.c plugin.c script.c static.c storage.c strutil.c \
		  subst.c type.c version.c
INCFILES	= array.c bignums.c dirprim.c interns.c iomonad.c \
		  preds.c printer.c scc.c timeprim.c timer.c
YFILES		= parser.y
SOURCES		= $(HFILES) $(CFILES) $(INCFILES) $(YFILES) 
OBJECTS		= builtin.$(OBJEXT) char.$(OBJEXT) compiler.$(OBJEXT) \
		  errors.$(OBJEXT) evaluator.$(OBJEXT) ffi.$(OBJEXT) \
		  goal.$(OBJEXT) input.$(OBJEXT) machdep.$(OBJEXT) \
		  machine.$(OBJEXT) module.$(OBJEXT) opts.$(OBJEXT) \
		  output.$(OBJEXT) plugin.$(OBJEXT) script.$(OBJEXT) \
		  static.$(OBJEXT) storage.$(OBJEXT) strutil.$(OBJEXT) \
		  subst.$(OBJEXT) type.$(OBJEXT) version.$(OBJEXT)
IOBJECTS	= hugs.$(OBJEXT) edit.$(OBJEXT) observe.$(OBJEXT) $(OBJECTS)
WOBJECTS	= @WOBJECTS@
RC_FILES	= @RC_FILES@
PROGRAMS	= hugs$(EXEEXT) runhugs$(EXEEXT) ffihugs$(EXEEXT) @COREHUGS@

################################################################
# Default target
################################################################

# This rule goes first to make it the default choice
default		:: all

all		:: $(PROGRAMS)

################################################################
# Hugs interpreter and standalone evaluator
################################################################

hugs$(EXEEXT)	: $(IOBJECTS) $(WOBJECTS) $(RC_FILES)
		  $(CC) $(LDFLAGS) $(IOBJECTS) $(WOBJECTS) $(LIBS) -o hugs$(EXEEXT) 
		  $(STRIP)

corehugs$(EXEEXT): corehugs.$(OBJEXT) $(OBJECTS) $(WOBJECTS) $(RC_FILES)
		  $(CC) $(LDFLAGS) corehugs.$(OBJEXT) $(OBJECTS) $(WOBJECTS) $(LIBS) \
			edit.$(OBJEXT) observe.$(OBJEXT) -o corehugs$(EXEEXT) 
		  $(STRIP)


version.$(OBJEXT) : version.c $(SOURCES)
		  @$(CC) \
		      -DMONTH_YEAR='"'"`date +'%B %Y' 2> /dev/null`"'"' \
		      -DYYYYMMDD='"'"`date +'%Y%m%d' 2> /dev/null`"'"' \
		      -c $(CFLAGS) $(OPTFLAGS) $<

SERVER_OBJECTS	= server.$(OBJEXT) $(OBJECTS) 
runhugs$(EXEEXT) : runhugs.$(OBJEXT) $(SERVER_OBJECTS)
		  $(CC) $(LDFLAGS) runhugs.$(OBJEXT) $(SERVER_OBJECTS) $(LIBS) -o runhugs$(EXEEXT)


ffihugs$(EXEEXT) : ffihugs.$(OBJEXT) $(SERVER_OBJECTS)
		  $(CC) $(LDFLAGS) ffihugs.$(OBJEXT) $(SERVER_OBJECTS) $(LIBS) -o ffihugs$(EXEEXT)

ffihugs.$(OBJEXT) : runhugs.$(OBJEXT)
		  $(CC) -c $(CFLAGS) $(OPTFLAGS) -DFFI_COMPILER runhugs.c -o ffihugs.$(OBJEXT)

################################################################
# Clean, distclean, veryclean, TAGS
################################################################

clean		::
		  $(RM) *.o
		  $(RM) *.O
		  $(RM) *.obj
		  $(RM) *.OBJ
		  $(RM) *.LIB
		  $(RM) *.DEF
		  $(RM) *.RES
		  $(RM) *.EXP
		  $(RM) *.ILK
		  $(RM) *.PDB
		  $(RM) *.TD2
		  $(RM) *.MAP
		  $(RM) *.CSM
		  $(RM) *.TR2
		  $(RM) *.DSW
		  $(RM) *.aux
		  $(RM) *.hp
distclean	:: clean
distclean	::
		  $(RM) $(PROGRAMS)
		  $(RM) Makefile
		  $(RM) config.h
		  $(RM) options.h
		  $(RM) platform.h
		  $(RM) *.pdf
		  $(RM) TAGS tags
		  $(RM) *~
veryclean	:: distclean
veryclean	::
TAGS		::
		  etags *.[ych]

################################################################
# C and Yacc rules
################################################################

.c.$(OBJEXT)	:
		  $(CC) -c $(CFLAGS) $(OPTFLAGS) $<

# Modules to be compiled without optimization.
# (old comment: to avoid optimisation bugs in certain compilers.
#               This may be overly conservative on some compilers.)
# (The following explanation is based on a posting by Alastair Reid.)
# These modules allocate cells on the Hugs heap and assume a conservative
# garbage collector.  On some (especially RISC) architectures, the
# optimizer may identify a pointer to a Cell as a common subexpression,
# and hold that instead of the Cell.  This would then be missed by the
# conservative garbage collector's simplistic scan of the C stack.
# Modules associated with evaluation are safe because they don't assume
# conservative GC (see IMPORTANT NOTICE in builtin.c).
compiler.$(OBJEXT) : compiler.c
		  $(CC) -c $(CFLAGS) compiler.c
module.$(OBJEXT) : module.c
		  $(CC) -c $(CFLAGS) module.c
subst.$(OBJEXT)	: subst.c
		  $(CC) -c $(CFLAGS) subst.c
static.$(OBJEXT) : static.c
		  $(CC) -c $(CFLAGS) static.c
type.$(OBJEXT)	: type.c
		  $(CC) -c $(CFLAGS) type.c

parser.c	: parser.y
		  -$(YACC) parser.y
		  mv y.tab.c parser.c
# veryclean	::
# 		  $(RM) parser.c

################################################################
# Generating object dependencies (requires gcc)
################################################################

HUGS_MAKEFILES = Makefile dotnet/Makefile msc/Makefile winhugs/Makefile

# optional definitions that govern include files
EXTRA_DEFINES = -DINTERNAL_PRIMS -DUNICODE_CHARS -DWANT_TIMER

depend: $(SOURCES) parser.c
	gcc -MM $(EXTRA_DEFINES) $(CFILES) | sed 's/\.o:/.$$(OBJEXT):/' >TMP
	if cmp -s TMP MkDepend.in; then : ; else \
		$(CP) TMP MkDepend.in; \
		for mfile in $(HUGS_MAKEFILES);\
		do	(echo '/^# Generated object dependencies/+++,/^# End of generated object dependencies/---d';\
			 echo '-r MkDepend.in';\
			 echo w) | ed - $$mfile; \
		done;\
	fi
	rm TMP

################################################################
# Generated object dependencies (Don't change or delete this line)
################################################################

@MkDepend@

################################################################
# End of generated object dependencies (Don't change or delete this line)
################################################################

@MkInstall@

################################################################
# End of Makefile
################################################################
